package com.liuzhongni.wahahanews.game;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.liuzhongni.wahahanews.R;

//import com.example.saizi.R.id;

public class Saizi extends Activity implements OnClickListener {
    private ImageView imView1;
    private ImageView imView2;
    private ImageView imView3;
    private ImageView imageViewHelp;
    private TextView textViewTongzhi;
    private Button start;
    private Button end;
    private Button btnBig;
    private Button btnSmall;
    private Button btnBaozi;
    private Handler handler1;
    private Handler handler2;
    private Handler handler3;
    private Runnable update1;
    private Runnable update2;
    private Runnable update3;
    private LinearLayout linearLayout;
    private LinearLayout linearLayoutStart;
    private int s1;
    private int s2;
    private int s3;
    private int total;
    private String result = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_saizi);
        getViews();
        handler1 = new Handler();
        handler2 = new Handler();
        handler3 = new Handler();

        one();
        two();
        three();
        btnBig.setOnClickListener(this);
        btnSmall.setOnClickListener(this);
        btnBaozi.setOnClickListener(this);
        start.setOnClickListener(this);
        end.setOnClickListener(this);
        imageViewHelp.setOnClickListener(this);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_saizi, menu);
        return true;
    }

    private void getViews() {
        imView1 = (ImageView) findViewById(R.id.imageView1);
        imView2 = (ImageView) findViewById(R.id.imageView2);
        imView3 = (ImageView) findViewById(R.id.imageView3);
        start = (Button) findViewById(R.id.main_btn_start);
        end = (Button) findViewById(R.id.main_btn_stop);
        btnBig  = (Button) findViewById(R.id.saizi_big);
        btnSmall  = (Button) findViewById(R.id.saizi_small);
        btnBaozi  = (Button) findViewById(R.id.saizi_baozi);
        imageViewHelp = (ImageView) findViewById(R.id.saizi_bangzhu);
        textViewTongzhi = (TextView) findViewById(R.id.saizi_tongzhi);
        linearLayout = (LinearLayout) findViewById(R.id.saizi_guess);
        linearLayoutStart = (LinearLayout) findViewById(R.id.saizi_start);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.saizi_bangzhu:
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                View view = LayoutInflater.from(this).inflate(R.layout.activity_saizi_bangzhu,null);
                builder.setView(view);
                builder.setPositiveButton("我知道了",null);
                builder.show();
                break;
            case R.id.saizi_big:
                textViewTongzhi.setText("您选择的是大");
                result = "大";
                yincang();
                xianshenStart();
                break;
            case R.id.saizi_small:
                textViewTongzhi.setText("您选择的是小");
                result = "小";
                yincang();
                xianshenStart();
                break;
            case R.id.saizi_baozi:
                textViewTongzhi.setText("您选择的是豹子");
                result = "豹子";
                yincang();
                xianshenStart();
                break;
            case R.id.main_btn_start:
                handler1.post(update1);
                handler2.post(update2);
                handler3.post(update3);
                total = 0;
                start.setEnabled(false);
                end.setEnabled(true);
                break;
            case R.id.main_btn_stop:
                handler1.removeCallbacks(update1);
                handler2.removeCallbacks(update2);
                handler3.removeCallbacks(update3);
                total = s1 + s2 + s3;
                if (s1 == s2 && s2 == s3) {
                    if(result.equals("豹子")){
                        textViewTongzhi.setText(total+"点 豹子！恭喜您！蒙对了！");
                    }else {
                        textViewTongzhi.setText(total+"点 豹子！很遗憾！蒙错了！");
                    }
                } else if (total >= 3 && total <= 10) {
                    if(result.equals("小")){
                        textViewTongzhi.setText(total+"点 小！恭喜您！蒙对了！");
                    }else {
                        textViewTongzhi.setText(total+"点 小！很遗憾！蒙错了！");
                    }
                } else if (total > 10) {
                    if(result.equals("大")){
                        textViewTongzhi.setText(total+"点 大！恭喜您！蒙对了！");
                    }else {
                        textViewTongzhi.setText(total+"点 大！很遗憾！蒙错了！");
                    }
                }
                start.setEnabled(true);
                xianshen();
                yincangStart();
                break;
        }
    }

    private void yincang() {
        linearLayout.setVisibility(View.INVISIBLE);
        end.setEnabled(false);
    }
    private void xianshen(){
        linearLayout.setVisibility(View.VISIBLE);
    }
    private void yincangStart(){
        linearLayoutStart.setVisibility(View.INVISIBLE);
    }
    private void xianshenStart(){
        linearLayoutStart.setVisibility(View.VISIBLE);
    }

    private void three() {
        update3 = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                s3 = (int)(Math.random() * 6+1);
                switch (s3) {
                    case 1:
                        imView3.setImageResource(R.mipmap.sai1);
                        break;
                    case 2:
                        imView3.setImageResource(R.mipmap.sai2);

                        break;
                    case 3:
                        imView3.setImageResource(R.mipmap.sai3);

                        break;
                    case 4:
                        imView3.setImageResource(R.mipmap.sai4);

                        break;
                    case 5:
                        imView3.setImageResource(R.mipmap.sai5);

                        break;
                    case 6:
                        imView3.setImageResource(R.mipmap.sai6);

                        break;

                    default:
                        break;
                }
                handler3.postDelayed(update3, 100);
            }
        };
    }

    private void two() {
        update2 = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                s2 = (int)(Math.random() * 6+1);
                switch (s2) {
                    case 1:
                        imView2.setImageResource(R.mipmap.sai1);
                        break;
                    case 2:
                        imView2.setImageResource(R.mipmap.sai2);

                        break;
                    case 3:
                        imView2.setImageResource(R.mipmap.sai3);

                        break;
                    case 4:
                        imView2.setImageResource(R.mipmap.sai4);

                        break;
                    case 5:
                        imView2.setImageResource(R.mipmap.sai5);

                        break;
                    case 6:
                        imView2.setImageResource(R.mipmap.sai6);

                        break;

                    default:
                        break;
                }
                handler2.postDelayed(update2, 100);
            }
        };
    }

    private void one() {
        update1 = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                s1 = (int)(Math.random() * 6+1);
                switch (s1) {
                    case 1:
                        imView1.setImageResource(R.mipmap.sai1);
                        break;
                    case 2:
                        imView1.setImageResource(R.mipmap.sai2);

                        break;
                    case 3:
                        imView1.setImageResource(R.mipmap.sai3);

                        break;
                    case 4:
                        imView1.setImageResource(R.mipmap.sai4);

                        break;
                    case 5:
                        imView1.setImageResource(R.mipmap.sai5);

                        break;
                    case 6:
                        imView1.setImageResource(R.mipmap.sai6);

                        break;

                    default:
                        break;
                }
                handler1.postDelayed(update1, 100);
            }
        };
    }

}
